 package srdb.migration.common;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
 
 public class DDLScriptFileFactory
 {
   private String path;
   private String schemaName;
   private static DDLScriptFileFactory filefactory;
   private PrintWriter tableWriter = null;
   private PrintWriter procedureWriter = null;
   private PrintWriter functionWriter = null;
   private PrintWriter packageWriter = null;
   private PrintWriter sequenceWriter = null;
   private PrintWriter synonymWriter = null;
   private PrintWriter triggerWriter = null;
   private PrintWriter schemaWriter = null;
   private PrintWriter viewWriter = null;
   private PrintWriter dbLinkWriter = null;
   private PrintWriter grantWriter = null;
   private PrintWriter userWriter = null;
   private PrintWriter objectTypeWriter = null;
   private PrintWriter ruleWriter = null;
   private PrintWriter groupWriter = null;
   private PrintWriter domainWriter = null;
   private PrintWriter dataTypeWriter = null;
 
   private PrintWriter masterWriter = null;
 
   private PrintWriter constraintWriter = null;
   private PrintWriter indexWriter = null;
 
   private StringBuilder pathBuilder = new StringBuilder();
 
   private DDLScriptFileFactory(String path, String schemaName)
   {
     this.path = path;
     this.schemaName = schemaName;
   }
 
   public static DDLScriptFileFactory getInstance(String path, String schemaName) {
     if ((filefactory == null) || (!filefactory.schemaName.equalsIgnoreCase(schemaName)) || (!filefactory.path.equals(path)))
     {
       filefactory = new DDLScriptFileFactory(path, schemaName);
     }
     return filefactory;
   }
 
   private String getFileName(MTKConstants.ObjectTypes type)
   {
     String temp = "";
     this.pathBuilder.delete(0, this.pathBuilder.length());
     this.pathBuilder.append(this.path);
     this.pathBuilder.append(File.separator);
     this.pathBuilder.append("mtk");
 
     if ((type != MTKConstants.ObjectTypes.USER) && (type != MTKConstants.ObjectTypes.ROLE) && (type != MTKConstants.ObjectTypes.GRANT))
     {
       this.pathBuilder.append("_").append(this.schemaName);
     }
 
     if (type != null) {
       this.pathBuilder.append("_");
       this.pathBuilder.append(type.toString());
     }
 
     this.pathBuilder.append("_ddl.sql");
     temp = this.pathBuilder.toString();
     return temp;
   }
 
   public PrintWriter getMasterFileWriter() throws IOException {
     if (this.masterWriter == null) {
       this.masterWriter = new PrintWriter(new FileOutputStream(getFileName(null)), true);
     }
 
     return this.masterWriter;
   }
 
   public PrintWriter getFileWriter(MTKConstants.ObjectTypes type)
     throws IOException
   {
     PrintWriter writer = null;
     switch (MTKConstants.ObjectTypes.values()[type.ordinal()])
     {
     case TABLE:
       if (this.tableWriter == null) {
         this.tableWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.tableWriter.println(getSearchPathString());
       }
       writer = this.tableWriter;
       break;
     case CONSTRAINT:
       if (this.constraintWriter == null) {
         this.constraintWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.constraintWriter.println(getSearchPathString());
       }
       writer = this.constraintWriter;
       break;
     case INDEX:
       if (this.indexWriter == null) {
         this.indexWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.indexWriter.println(getSearchPathString());
       }
       writer = this.indexWriter;
       break;
     case PROCEDURE:
       if (this.procedureWriter == null) {
         this.procedureWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.procedureWriter.println(getSearchPathString());
       }
       writer = this.procedureWriter;
       break;
     case TRIGGER:
       if (this.triggerWriter == null) {
         this.triggerWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.triggerWriter.println(getSearchPathString());
       }
       writer = this.triggerWriter;
       break;
     case FUNCTION:
       if (this.functionWriter == null) {
         this.functionWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.functionWriter.println(getSearchPathString());
       }
       writer = this.functionWriter;
       break;
     case PACKAGE:
       if (this.packageWriter == null) {
         this.packageWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.packageWriter.println(getSearchPathString());
       }
       writer = this.packageWriter;
       break;
     case SYNONYM:
       if (this.synonymWriter == null) {
         this.synonymWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.synonymWriter.println(getSearchPathString());
       }
       writer = this.synonymWriter;
       break;
     case SEQUENCE:
       if (this.sequenceWriter == null) {
         this.sequenceWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.sequenceWriter.println(getSearchPathString());
       }
       writer = this.sequenceWriter;
       break;
     case SCHEMA:
       if (this.schemaWriter == null) {
         this.schemaWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.schemaWriter;
       break;
     case VIEW:
       if (this.viewWriter == null) {
         this.viewWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.viewWriter.println(getSearchPathString());
       }
       writer = this.viewWriter;
       break;
     case DBLINK:
       if (this.dbLinkWriter == null) {
         this.dbLinkWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
         this.dbLinkWriter.println(getSearchPathString());
       }
 
       writer = this.dbLinkWriter;
       break;
     case GRANT:
       if (this.grantWriter == null) {
         this.grantWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
 
       writer = this.grantWriter;
       break;
     case ROLE:
     case USER:
       if (this.userWriter == null) {
         this.userWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.userWriter;
       break;
     case OBJECTTYPE:
       if (this.objectTypeWriter == null) {
         this.objectTypeWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.objectTypeWriter;
       break;
     case RULE:
       if (this.ruleWriter == null) {
         this.ruleWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.ruleWriter;
       break;
     case GROUP:
       if (this.groupWriter == null) {
         this.groupWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.groupWriter;
       break;
     case DOMAIN:
       if (this.domainWriter == null) {
         this.domainWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.domainWriter;
       break;
     case DATATYPE:
       if (this.dataTypeWriter == null) {
         this.dataTypeWriter = new PrintWriter(new FileOutputStream(getFileName(type)), true);
       }
       writer = this.dataTypeWriter;
     }
 
     return writer;
   }
 
   public String getSearchPathString() {
     return "SET search_path=" + this.schemaName + ";";
   }
 
   public void closeWriters()
   {
     if (this.masterWriter != null) {
       this.masterWriter.close();
       this.masterWriter = null;
     }
 
     if (this.tableWriter != null) {
       this.tableWriter.close();
       this.tableWriter = null;
     }
 
     if (this.constraintWriter != null) {
       this.constraintWriter.close();
       this.constraintWriter = null;
     }
 
     if (this.indexWriter != null) {
       this.indexWriter.close();
       this.indexWriter = null;
     }
 
     if (this.triggerWriter != null) {
       this.triggerWriter.close();
       this.triggerWriter = null;
     }
 
     if (this.functionWriter != null) {
       this.functionWriter.close();
       this.functionWriter = null;
     }
 
     if (this.procedureWriter != null) {
       this.procedureWriter.close();
       this.procedureWriter = null;
     }
 
     if (this.packageWriter != null) {
       this.packageWriter.close();
       this.packageWriter = null;
     }
 
     if (this.schemaWriter != null) {
       this.schemaWriter.close();
       this.schemaWriter = null;
     }
 
     if (this.synonymWriter != null) {
       this.synonymWriter.close();
       this.synonymWriter = null;
     }
 
     if (this.sequenceWriter != null) {
       this.sequenceWriter.close();
       this.sequenceWriter = null;
     }
 
     if (this.viewWriter != null) {
       this.viewWriter.close();
       this.viewWriter = null;
     }
 
     if (this.dbLinkWriter != null) {
       this.dbLinkWriter.close();
       this.dbLinkWriter = null;
     }
 
     if (this.grantWriter != null) {
       this.grantWriter.close();
       this.grantWriter = null;
     }
 
     if (this.userWriter != null) {
       this.userWriter.close();
       this.userWriter = null;
     }
 
     if (this.objectTypeWriter != null) {
       this.objectTypeWriter.close();
       this.objectTypeWriter = null;
     }
 
     if (this.ruleWriter != null) {
       this.ruleWriter.close();
       this.ruleWriter = null;
     }
 
     if (this.groupWriter != null) {
       this.groupWriter.close();
       this.groupWriter = null;
     }
 
     if (this.domainWriter != null) {
       this.domainWriter.close();
       this.domainWriter = null;
     }
 
     if (this.dataTypeWriter != null) {
       this.dataTypeWriter.close();
       this.dataTypeWriter = null;
     }
   }
 }

/* 

 * 
 */